package main

import (
	"fmt"
	"math"
)

func solution(n int, a []int) int {
    maxValue := 0
    for i := 0; i < n; i++ {
        for j := i + 1; j < n; j++ {
            // 计算环形距离
            dist := j - i
            if dist > n/2 {
                dist = n - dist
            }
            currentValue := (a[i] + a[j]) * dist
            if currentValue > maxValue {
                maxValue = currentValue
            }
        }
    }
    return maxValue
}

func solution1(n int, a []int) int {
	maxValue := 0
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			dist := math.Min(math.Abs(float64(j-i)), float64(n)-math.Abs(float64(j-i)))
			currentValue := (a[i] + a[j]) * int(dist)
			if currentValue > maxValue {
				maxValue = currentValue
			}
		}
	}
	return maxValue
}

func main() {
	fmt.Println(solution(3, []int{1, 2, 3}) == 5)
	fmt.Println(solution(4, []int{4, 1, 2, 3}) == 12)
	fmt.Println(solution(5, []int{1, 5, 3, 7, 2}) == 24)
}